% 这一部分检查调制器和解调器的输出是否正确: 包括PLL/FLL/DLL 能否正确跟踪

% 信号调制部分
% 这一部分参考 ref/Glockenspiel的内容，尽量简化(采用GPS)
% 调制中doppler采用 恒大多普勒频率/恒定多普勒频移变化率等
% 注意信号信噪/载噪比的选取的问题

%% 1. 加载配置（信号+接收机）
% IF_carrierFreq = signalConfig.signal.CenterFrequency; % 中频载波频率(Hz)
sim_totalDuration = signalConfig.sim.duration;   % 总仿真时长
sim_startTime = 0;      % 开始时间
sim_interval = rxConfig.tracker.totalIntegTime;   % 采样间隔(秒)
M = signalConfig.antennas.count;                  % 天线数量(M=4)

%% 2. 初始化模型
modulatorModel = SignalModulator(signalConfig, rxConfig); 
demodulatorModel = RxTrackingLoop(signalConfig, rxConfig);  

%% 3. 调制和解调
% 预计算总步数（与类的total参数匹配）
totalSteps = round((sim_totalDuration) / sim_interval) + 1;

%% 4. 初始化进度显示类
progressDisplayer = ProgressDisplayer(...
    '调制解调测试仿真：', ...  % 自定义标题
    totalSteps, ...            % 总步数（与循环步数一致）
    100, ...                   % 进度刻度（100表示百分比，可改为总步数显示）
    false);                    % 不禁用显示

progressDisplayer.start();

%% 5. 初始化记录变量
fqyerrList = [];
pherrList = [];
delayerrList = [];
fqyList = [];
phList = [];
delayList = [];
cndrList = [];
rotfreqList = [];
rotAList = [];
rotAlphaList = [];
tMeasureList = [];  
phlockoList = [];
phlockpList = [];

% 循环仿真
for step = 1:totalSteps
    % 计算当前仿真时间（业务逻辑用，进度显示由类自动处理）
    t = sim_startTime + (step - 1) * sim_interval;
    
    % 仿真核心步骤（原有代码不变）
    [waveformRx, tRx] = modulatorModel.step();
    [result, performationIndex] = demodulatorModel.step(waveformRx);
    
    % 【核心修改】更新进度（每次步长+1，类自动控制刷新频率）
    progressDisplayer.step(1);  % delta_progress=1，每次循环进度+1

    % 内容记录
    fqyerr =    result.frequencyError;
    pherr =     result.phaseError;
    delayerr =  result.codeError;
    fqy =       result.frequency ;
    ph =             result.phase;
    delay =             result.delay ;
    cndr =             result.cndr ;

    rotfreq =             result.rotFreqEst;
    rotA =             result.rotAEst;
    rotAlpha =             result.rotAlphaEst;

    tMeasure =             performationIndex.tMeasure;
    % integ_coh_p_cob =             performationIndex.integ_coh_p_cob;
    phlocko =             performationIndex.phlocko; 
    phlockp =             performationIndex.phlockp;

    fqyerrList  = [fqyerrList; fqyerr];
    pherrList  = [pherrList; pherr];
    delayerrList  = [delayerrList; delayerr];
    fqyList  = [fqyList; fqy];
    phList  = [phList; ph];
    delayList  = [delayList; delay];
    cndrList  = [cndrList; cndr];
    rotfreqList  = [rotfreqList; rotfreq];
    % rotAList  = [rotAList; rotA];
    % rotAlphaList  = [rotAlphaList; rotAlpha];
    tMeasureList  = [tMeasureList; tMeasure];
    phlockoList  = [phlockoList; phlocko];
    phlockpList  = [phlockpList; phlockp];
end

%% 6. 绘制结果
% 真值计算
[phGenie, fqyGenie, delayGenie, cndrGenie] = getGenie(signalConfig,rxConfig ,tMeasureList);
% 绘制估计的频率、相位、延迟 与对应真值的对比图
winLen = 32;
fontsize = 12;
groupTitle = '调制解调测试';
plotPLL(groupTitle, tMeasureList, pherrList, phList, phGenie, phlockoList, phlockpList, winLen, fontsize);
plotFLL(groupTitle, tMeasureList, fqyerrList,  fqyList, fqyGenie, winLen, fontsize);
plotDLL(groupTitle, tMeasureList, delayerrList, delayList, delayGenie, signalConfig.signal.codeLen, winLen, fontsize);
% plotCndr(groupTitle, tMeasureList, cndrList, cndrGenie, fontsize);

% 绘制旋转频率、旋转A、旋转Alpha
% figure;
% subplot(3,1,1);
% plot(tMeasureList, rotfreqList);
% title('旋转频率');
% xlabel('时间 (s)');
% ylabel('旋转频率 (Hz)');
% 
% subplot(3,1,2);
% plot(tMeasureList, rotAList);
% title('旋转A');
% xlabel('时间 (s)');
% ylabel('旋转相位参数A估计');
% 
% subplot(3,1,3);
% plot(tMeasureList, rotAlphaList);
% title('旋转\alpha');
% xlabel('时间 (s)');
% ylabel('旋转相位参数Alpha估计');


